<?php

class Admin_Model_Menu {

    private $_dbtable;

    public function __construct() {
        $this->_dbtable = new Admin_Model_DbTable_Menu();
    }

    /**
     * 
     */
    public function fetchAllData() {
        $select = $this->_dbtable->select();
        return $this->_dbtable->fetchAll($select);
    }

    /**
     * 
     * @param type $id
     */
    public function getMenuById($id) {
        $select = $this->_dbtable->select();
        $select->where("id = $id");
        return $this->_dbtable->fetchAll($select);
    }

    public function getMenuInArrayFormat($data) {
        
    }

    /**
     * 
     * @param type $data
     */
    public function createNewMenu($data) {
        return $this->_dbtable->insert($data);
    }

    /**
     * 
     * @param type $data
     * @param type $id
     */
    public function updateMenu($data, $id) {
        return $this->_dbtable->update($data, "id = $id");
    }

    /**
     * 
     * @param type $visible
     * @return type
     */
    public function getParents($visible) {
        $select = $this->_dbtable->select();
        $select->where("parent_id = 0");
        if ($visible != null) {
            $select->where("visible = $visible");
        }
        return $this->_dbtable->fetchAll($select);
    }

    /**
     * 
     * @param type $parent_id
     * @param type $visible
     * @return type
     */
    public function getChilds($parent_id, $visible) {
        $select = $this->_dbtable->select();
        $select->where("parent_id = $parent_id");
        if ($visible != null) {
            $select->where("visible = $visible");
        }
        return $this->_dbtable->fetchAll($select);
    }

    /**
     * 
     * @param type $menuId
     * @return type
     */
    public function getParentOfMenuId($menuId) {
        $childMenu = $this->getMenuById($menuId);
        $parentId = $childMenu[0]['parent_id'];
        if ($parentId != 0) {
            $parentMenu = $this->getMenuById($parentId);
            return $parentMenu[0];
        }
        return $childMenu[0];
    }

}

